<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>更改用户密码</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<!--<link rel="stylesheet" type="text/css" href="css/bootstrap-switch.min.css" />-->
<!--<link rel="stylesheet" type="text/css" href="css/addContract.css" />-->


<script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
    <script type="text/javascript" src="layer/layer.js"></script>
    <script type="text/javascript" src="js/handlebars-v3.0.3.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/mui.min.js"></script>
    <script type="text/javascript" src="js/security.min.js"></script>
    <script type="text/javascript" src="js/md5.js"></script>
</head>

<body>
    <div class="addMain" style="margin:0px auto;width:266px;height:200px;">
    	<div class="add-detail" id="divOldPass">
        	<span id="spanOldPass">当前密码：</span><input type="password" id="oldPassword" class="add-text" />
        </div>
        <div class="add-detail">
        	新的密码：<input type="password" id="newPassword" class="add-text" />
        </div>
        <div class="add-detail-date">
        	确认密码：<input type="password" id="newPassword2" class="add-text" />
        </div>
        <div class="">
            <input type="button" id="btn" class="add-receive"
                   style="height:23px;width:84px;line-height:20px;font-size:12px;" value="保存"/>
        </div>  
    </div>
</body>
</html>
<script type="text/javascript">
window.onload=function(){
    var modulus="009a082e53695b61cbdbd4dd5dd374a87b842d82660843ed79047868301c6f65647e5bcffcae39d81be2d96d46981d8d92d6ee344969f3dab0a763e84989296f5b3a7975ec20d19c815794760a1f8c203effa52ca465f92a639181f2f3097cb8f1402dc735ebc0466fc80675f54efbb63219980f39e8a7c4374d3877a8cb8e187d", exponent="010001";
    var hosturl = "http://"+ window.location.host + "/"+ window.location.pathname.split('/')[1];
    var btn=document.getElementById('btn');
    var n=self.location.href.indexOf("?uid")//查看是否包含参数
    if(n>0)//存在参数
    {
        var uid = self.location.href.substr(n + 5)+"";
        document.getElementById('divOldPass').parentNode.removeChild(document.getElementById('divOldPass'));
    }
    btn.addEventListener("click",function() {
        var newPassword = document.getElementById('newPassword').value;
        var newPassword2 = document.getElementById('newPassword2').value;
        if(newPassword==""||newPassword2==""){
            layer.msg('输入的密码不能为空！', function(){
            });
            return;
        }
        if (newPassword != newPassword2) {
            layer.msg('两次输入的密码不一致！', function(){
            });
            return;
        }
        if(uid==null){
            var oldPassword = document.getElementById('oldPassword').value;
            if(oldPassword==""){
                layer.msg('旧密码不能为空！', function(){
                });
                return;
            }
            var key = RSAUtils.getKeyPair(exponent, '', modulus);
            var encryptedPwd = RSAUtils.encryptedString(key, newPassword);
            var oldPassword = hex_md5(oldPassword+"cmaracc");
            var newPassword = hex_md5(newPassword+"cmaracc");
            mui.ajax(hosturl+"/user/change", {
                data: {
                    "oldPassword": oldPassword,
                    "newPassword": newPassword
                },
                dataType: "json",
                type: "post",
                timeout: 10000,
                success: function (datas) {
                    if (datas.status == "ok") {
                        parent.$("#handle_status").val('OK');
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    } else {
                        layer.msg(datas.description, function(){
                        });
                        return;
                    }
                },
                error: function (xhr, type, errorThrown) {
                    console.log(type);
                }
            });
        }else{
            var key = RSAUtils.getKeyPair(exponent, '', modulus);
            var encryptedPwd = RSAUtils.encryptedString(key, newPassword);
            var newPassword = hex_md5(newPassword+"cmaracc");
            mui.ajax(hosturl+"/user/changePassByAdmin", {
                data: {
                    "userId": uid,
                    "newPassword": newPassword
                },
                dataType: "json",
                type: "post",
                timeout: 10000,
                success: function (datas) {
                    if (datas.status == "ok") {

                        parent.$("#handle_status").val('OK');
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);

                        //window.location.reload();
                    } else {
                        parent.$("#handle_status").val('FAIL');
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);


                        //window.location.reload();
                    }
                },
                error: function (xhr, type, errorThrown) {
                    console.log(type);
                }
            });
        }

    });

	};


    function GetQueryString(name) {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);

        if (r != null)return unescape(r[2]);
        return null;
    }
    ;
</script>

</body>
</html>
